package com.neusoft.neumooc.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.core.common.BaseDAO;
import com.neusoft.core.common.exception.DAOException;
import com.neusoft.neumooc.vo.UserVO;

/**
 * @ClassName: UserDaoImpl
 * @Description:用户数据访问类
 * @author lusiyu
 * @date 2016年8月16日
 */
@Repository
public class UserDAOImpl extends BaseDAO implements UserDAO {

	//获取用户
	private final static String QUERY_USER = "select * from cw_users where userName=?";
	/**
	 * @Title: getUserByName
	 * @Description:通过登录名获取用户
	 * @param userName 用户名
	 * @return Map<String, Object> 用户信息
	 * @throws DAOException
	 */
	@Override
	public Map<String, Object> getUserByName(String userName) throws DAOException {
		Object[] obj = { userName };
		return this.queryForMap(QUERY_USER, obj);
	}

	//查询用户信息
	private final static String QUERR_USER_SQL = "select cw_users.userId,cw_users.userName,cw_users.roleId,cw_users.buildDate,cw_users.lastLoginDate,cw_users.status,count(cw_contents.createUser) as count,if(cw_users.roleId=9,'管理员','信息员') as roleValue,if(cw_users.status=1,'可用','停用') as statusValue from cw_users left join cw_contents on cw_users.userName=cw_contents.createUser group by cw_users.userId";

	//删除用户
	private final static String QUERR_DELETE_USER_SQL = "DELETE  FROM cw_users WHERE userId=?";

	//通过id查询用户信息
	private final static String SQL_GET_USER_INFO = "SELECT *FROM cw_users WHERE userId=?";
	/**
	 * @Title: getUserInfo
	 * @Description:通过id获取用户信息
	 * @param userId 用户信息
	 * @throws DAOException
	 */
	@Override
	public UserVO getUserInfo(String userId) throws DAOException {
		return this.queryForBean(SQL_GET_USER_INFO, UserVO.class, new Object[] { userId });
	}

	// 获得列表个数 
	private final static String QUERR_USERNUM_SQL = "SELECT COUNT(*)FROM cw_users";
	/**
	 * @Title: queryUserNum
	 * @Description:获取用户列表
	 * @throws DAOException
	 */
	@Override
	public int queryUserNum() throws DAOException {
		return this.queryForInteger(QUERR_USERNUM_SQL);
	}

	/**
	 * @Title: getUserByName
	 * @Description:获取列表
	 * @param pageNo,pageSize
	 * @return List 列表
	 * @throws DAOException
	 */
	@Override
	public List<UserVO> getUsers(int pageNo, int pageSize) throws DAOException {
		return this.getPageData(QUERR_USER_SQL, UserVO.class, pageNo, pageSize);
	}

	//添加用户
	private final static String QUERR_ADD_USER_SQL = "INSERT INTO "
			+ "cw_users(userId,userName,password,roleId,buildDate,lastLoginDate,status)"
			+ "values(:userId, :userName, :password, :roleId,now(),now(),'0')";
	/**
	 * @Title: addUser
	 * @Description:添加用户信息
	 * @param userVO
	 * @return boolean 成功、失败
	 * @throws DAOException
	 */
	@Override
	public boolean addUser(UserVO userVO) throws DAOException {
		int flag = this.update(QUERR_ADD_USER_SQL, userVO);
		if (flag == 1) {
			return true;
		}
		return false;
	}

	//更新用户信息
	private final static String SQL_UPDATE_USER = "UPDATE cw_users SET "
			+ "password=:password,roleId=:roleId,buildDate=now(),status=:status "
			+ "where userId=:userId";
	/**
	 * @Title: updateUser
	 * @Description:更新用户信息
	 * @param userVO
	 * @return boolean 成功、失败
	 * @throws DAOException
	 */
	@Transactional
	public boolean updateUser(UserVO userVO) throws DAOException {
		int flag = this.update(SQL_UPDATE_USER, userVO);
		if (flag == 1) {
			return true;
		}
		return false;

	}

	/**
	 * @Title: getUser
	 * @Description:通过id查询列表
	 * @param userId 用户id
	 * @return Map<String, Object>
z	 */
	@Override
	public Map<String, Object> getUser(String userId) {
		return this.queryForMap(SQL_GET_USER_INFO, new Object[] { userId });
	}

	/**
	 * @Title: deleteUser
	 * @Description:通过id删除用户
	 * @param userId 用户id
	 * @return int
z	 */
	@Override
	public int deleteUser(String userId) {
		return this.update(QUERR_DELETE_USER_SQL, new Object[] { userId });
	}

	/**
	 * @Title: updateStatusNo
	 * @Description:启用用户
	 * @param userId
	 * @return void 1
	 * @throws DAOException
	 */
	@Override
	public void updateStatusOn(String userId) throws DAOException {
		String SQL_UPDATE_STATUS_NO = "update cw_users set status='1'";
		String[] key_no_array = userId.split(",");
		SQL_UPDATE_STATUS_NO += "where userId in(";
		for (int i = 0; i < key_no_array.length; i++) {
			SQL_UPDATE_STATUS_NO += "'" + key_no_array[i] + "'";
			if (key_no_array.length - 1 != i) {
				SQL_UPDATE_STATUS_NO += ",";
			}
		}
		SQL_UPDATE_STATUS_NO += ")";
		this.update(SQL_UPDATE_STATUS_NO);
	}

	/**
	 * @Title: updateStatusOff
	 * @Description:停用用户
	 * @param userId
	 * @return void 0
	 * @throws DAOException
	 */
	@Override
	public void updateStatusOff(String userId) throws DAOException {
		String SQL_UPDATE_STATUS_OFF = "update cw_users set status='0'";
		String[] key_no_array = userId.split(",");
		SQL_UPDATE_STATUS_OFF += "where userId in(";
		for (int i = 0; i < key_no_array.length; i++) {
			SQL_UPDATE_STATUS_OFF += "'" + key_no_array[i] + "'";
			if (key_no_array.length - 1 != i) {
				SQL_UPDATE_STATUS_OFF += ",";
			}
		}
		SQL_UPDATE_STATUS_OFF += ")";
		this.update(SQL_UPDATE_STATUS_OFF);
	}

}
